/**
 * 
 */
package np.com.abhishek.scrambler.utils;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

import np.com.abhishek.scrambler.ui.Initializer;
import np.com.abhishek.ui.WidgetFactory;

/**
 * @author Abhishek
 * 
 */
// this class can be used to play an audio file and plays .au and .wav formats
public class Sound {
	AudioInputStream stream = null;
	WidgetFactory widgetFactory = WidgetFactory.getInstance();

	public void playSound(int soundType) {
		try {
			if (soundType == Initializer.MOVE)
				stream = widgetFactory.createAdAudioInputStream("move.wav");

			else if (soundType == Initializer.ILLEGAL_MOVE)
				stream =widgetFactory.createAdAudioInputStream("illegal_move.wav");

			else if (soundType == Initializer.HAS_WON)
				stream = widgetFactory.createAdAudioInputStream("applause.wav");

			else if (soundType == Initializer.SCARMBLE)
				stream = widgetFactory.createAdAudioInputStream("solve.wav");

			else if (soundType == Initializer.GIVE_UP)
				stream = widgetFactory.createAdAudioInputStream("ching.wav");

			AudioFormat format = stream.getFormat();

			stream = AudioSystem.getAudioInputStream(format, stream);

			SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class,
					stream.getFormat(), ((int) stream.getFrameLength() * format
							.getFrameSize()));
			SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

			line.open(stream.getFormat());
			line.start();

			int numRead = stream.available();

			byte[] buf = new byte[numRead];

			stream.read(buf, 0, numRead);

			line.write(buf, 0, numRead);
			line.drain();
			line.stop();
		} catch (Exception e) {
		}
	}
}
